package com.smile.weather.presenter;

import android.content.Context;
import android.util.Log;

import com.smile.weather.bean.DayWeatherData;
import com.smile.weather.bean.HourWeatherData;
import com.smile.weather.interfaces.ISevenDayFragment;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by smile on 2016/8/23.
 */
public class SevenDayPresener {

    private Context mContext;
    private ISevenDayFragment iSevenDayFragment;
    private List<HourWeatherData> hourWeatherDatas;
    private List<DayWeatherData> dayWeatherDatas;

    public SevenDayPresener(Context mContext) {
        this.mContext = mContext;
    }

    public void setiSevenDayFragment(ISevenDayFragment iSevenDayFragment) {
        this.iSevenDayFragment = iSevenDayFragment;
    }

    public void analyzeHtml(String html) {

        dealHour(html);
        dealDay(html);
    }

    private void dealHour(String html){

        StringBuilder builder = new StringBuilder(html);
        builder.delete(0, builder.indexOf("hour3data=") + 10);
        builder.delete(builder.indexOf("</script>"), builder.length());
        builder.delete(0, builder.indexOf("[") + 1);
        builder.delete(builder.indexOf("]"), builder.length());
        String[] r = builder.toString().split("\",\"");
        hourWeatherDatas = new ArrayList<>();
        for(int i = 0;i < r.length;++i){
            String[] r2 = r[i].replaceAll("\"", "").split(",");
            HourWeatherData hourWeatherData = new HourWeatherData();
            hourWeatherData.setHour(r2[0]);
            hourWeatherData.setWeather_icon(r2[1]);
            hourWeatherData.setWeather(r2[2]);
            hourWeatherData.setTemperature(r2[3]);
            hourWeatherDatas.add(hourWeatherData);
        }
        iSevenDayFragment.setHourWeather(hourWeatherDatas);
    }

    private void dealDay(String html){

        Document content = Jsoup.parse(html);
        Elements element = content.getElementsByAttributeValue("class", "t clearfix");
        Elements e = element.get(0).getElementsByTag("li");
        Elements element2 = content.getElementsByClass("png40");
        dayWeatherDatas = new ArrayList<>();
        for(int i = 0;i < 7;++i){
            Elements e2 = e.get(i).children();
            DayWeatherData dayWeatherData = new DayWeatherData();
            String dates = e2.get(0).text();
            String date = dates.substring(0, dates.indexOf("（"));
            String date2 = dates.replace(date, "").replace("（", "").replace("）", "");
            dayWeatherData.setDate(date);
            dayWeatherData.setDate2(date2);
            dayWeatherData.setWeather(e2.get(3).text());
            dayWeatherData.setTemperature(e2.get(4).text());
            dayWeatherData.setWeather_icon1(element2.get(i * 2).className().replace("png40", "").trim());
            dayWeatherData.setWeather_icon2(element2.get(i * 2 + 1).className().replace("png40", "").trim());

            dayWeatherDatas.add(dayWeatherData);
        }
        iSevenDayFragment.setDayWeather(dayWeatherDatas);
    }

}
